<template>
	<view class="actionbar">
		<!-- 输入区域 -->
		<view class="send-box flex-between">
			<!-- 发送图片 -->
			<view class="left">
				<m-icon size="48" type="icon-tianjiazhaopian" color="var(--text_1)" @click="sendImg"
					:disabled="isDisabled"></m-icon>
			</view>
			<!-- 禁言状态 -->
			<view class="center">
				<view class="disabled flex-center" v-if="isDisabled">
					<text>禁言中</text>
				</view>
				<textarea @focus.stop="focus_input()" @keyboardheightchange="keyboardheightchange" :focus="focus"
					@confirm="sendMes" v-model="content" confirm-type="send" type="text" auto-height>
				</textarea>
				<!-- 引用 -->
				<view class="Incoming flex fs21 mt-12 " v-if="Object.keys(IncomingMsg).length">
					<text class="">{{IncomingMsg['tonickname']}}：</text>
					<view class="otp2 flex-1">
						<text v-if="JSON.parse(IncomingMsg['sencmsgcontent'])['type']==1">{{ JSON.parse(IncomingMsg['sencmsgcontent'])['content'] }}
						</text>
					</view>
					<m-icon type="icon-shanchu1" @click="$emit('setIncomingMsg')" />
				</view>
			</view>
			<!-- 加号  更多服务 -->
			<!-- <view class="right-01">
				<m-icon size="40" type="icon-tianjia" color="var(--text_1)" @click="sendMes"
					:disabled="isDisabled"></m-icon>
			</view> -->
			<view class="right flex-between" :class="{'active':content}">
				<template class="send">
					<m-button size="small" :loading="sendLoading" @click="sendMes">发送</m-button>
					<m-icon class="zone" size="40" type="icon-huojian" color="var(--text_1)" @click="sendMes"></m-icon>
				</template>
				<!-- <view class="zone flex-between" @click.stop="isOperaiton = !isOperaiton">
					<m-icon size="40" type="icon-tianjia" color="var(--text_1)" />
				</view> -->
			</view>
		</view>
		<!-- 操作区 -->
		<!-- 		<view class="operation" :class="{'operation-active':isOperaiton}" @click.stop="">
			<view class="card flex-around">
				<template>
					<uni-file-picker v-model="imageValue" fileMediatype="image" mode="grid" @success="onSuccess" />
				</template>
				<m-icon type="icon-tupian" size="100" @click="fileImage"></m-icon>
				<m-image src="" size="100"></m-image>
				<m-image src="" size="100"></m-image>
				<m-image src="" size="100"></m-image>
			</view>
			<view class="card flex-around">
				<m-image src="" size="100"></m-image>
				<m-image src="" size="100"></m-image>
				<m-image src="" size="100"></m-image>
				<m-image src="" size="100"></m-image>
			</view>
		</view> -->
	</view>
</template>

<script>
	import { Upload } from '@/api/Ctrls.js';
	import { mapState, mapMutations } from 'vuex'
	import { chooseImage, chooseFile } from '@/utils/tools.js'
	import { getmsgcontent, sendmsg } from "@/api/msg.js"
	export default {
		name: 'actionbar',
		data() {
			return {
				focus: false,
				imageValue: [],

				//发送内容
				content: '',
				sendLoading: false,
				isOperaiton: false,
				operaitonType: 0, // 0表示关闭  1表示正常点击开启 3表示获取焦点下开启
				isFocus: false,
			};
		},
		props: {
			msgType: {
				type: Number,
				default: 1, //  1-- 文字消息  2-- 图片 3--视频  4--商品
			},
			isDisabled: {
				type: Boolean,
				default: false, //是否禁言
			},
			touid: {
				type: Number,
				default: 8, // 
			},
			// 传入msg
			IncomingMsg: {
				type: Object,
				default: () => {}
			}
		},
		watch: {
			isOperaiton(val) {
				this.$emit('onOper', val)
			},
			IncomingMsg(val) {
				this.focus = false;
				console.log('val', val)
				this.focus_input()
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				allMes: state => state.chat.allMes,
				captain: state => state.chat.captain,
			}),
		},
		mounted() {
			this.focus_input()
		},
		methods: {
			...mapMutations({
				CLEAR_CAPTAIN: 'chat/CLEAR_CAPTAIN',
				Add_ONE: 'chat/Add_ONE',
				HISTORY_MES: 'chat/HISTORY_MES',
				SET_MES: 'chat/SET_MES',
				SET_CAPTAIN: 'chat/SET_CAPTAIN',
				SET_CAPTAINIDX: 'chat/SET_CAPTAINIDX',
				DELETE_ALLMES: 'chat/DELETE_ALLMES',
				SET_WAITMES: 'chat/SET_WAITMES',
				ADD_LOADING: 'chat/ADD_LOADING',
			}),
			focus_input() {
				setTimeout(() => {
					this.focus = true
				}, 100)
			},
			onSuccess() {

			},
			keyboardheightchange(e) {
				console.log('e===>', e.detail)
			},

			fileImage() {
				console.log(123)
			},
			// 发送消息
			async sendMes() {
				this.focus = false;
				let dataObj = {};
				const tokenid = Date.parse(new Date()) / 1000
				if (!this.content) return this.Toast('请输入内容')
				dataObj = {
					ctime: tokenid,
					type: 1,
					content: this.content,
				}
				// 添加引用信息
				if (Object.keys(this.IncomingMsg).length !== 0) {
					// const inMsg = {
					// 	msgtype: this.IncomingMsg.msgtype,
					// 	tonickname: this.IncomingMsg.tonickname,
					// 	content: this.IncomingMsg.sencmsgcontent.content,
					// }
					// dataObj.inMsg = inMsg;
					dataObj.inMsgId=this.IncomingMsg['id']
				}
				console.log('dataObj===>', dataObj)
				// 组装消息对象
				const msgs = {
					id: tokenid,
					fromheadpic: this.userinfo.headpicurl,
					fromnickname: this.userinfo.nickname,
					fromuid: this.userinfo.userid,
					isLoading: true,
					sencmsgcontent: JSON.stringify(dataObj)
				}
				console.log('msgs===>', msgs)
				this.Add_ONE(msgs);
				// this.ADD_LOADING(msgs);
				// await this.sleep(3000);
				this.sendmsg({
					touid: this.touid,
					msgtype: 1, // 1 文字  2 图片  3 产品 
					msgcontent: JSON.stringify(dataObj),
				})
				this.$emit('setIncomingMsg');
				this.focus_input()
			},
			// 发送图片消息
			async sendImg() {
				let dataObj = {};
				const tokenid = Date.parse(new Date()) / 1000
				try {
					dataObj = {
						ctime: tokenid,
						type: 2,
						content: '',
					}
					const { tempFilePaths, tempFiles } = await this.chooseFile();
					const msgs = {
						id: tokenid,
						fromheadpic: this.userinfo.headpicurl,
						fromnickname: this.userinfo.nickname,
						fromuid: this.userinfo.userid,
						isLoading: true,
						sencmsgcontent: JSON.stringify(dataObj)
					}
					//  本地组装消息发出
					this.Add_ONE(msgs);
					// 添加Loadining
					this.ADD_LOADING(msgs);
					// await this.sleep(10000)
					const { data, success: fileStatus, msg: fileMsg } = await Upload(tempFilePaths[0]);
					if (!fileStatus) {
						// this.Toast(fileMsg);
						// //删除错误的那条聊天信息
						// this.DELETE_ALLMES(msgs.id)
						// //数据不变改变 这条数据ID和发送状态 再存一份
						// msgs.id = this.guid();
						// msgs.isLoading = 'fail';
						// // 然后存储到总列表
						// this.Add_ONE(msgs);
						//发送失败的消息 将重新指向这个id的数据
						// this.SET_CAPTAINIDX({ id:msgs.id, idx:idx })
					} else {
						dataObj.content = data;
						// 发送图片消息
						this.sendmsg({
							touid: this.touid,
							msgtype: 2, // 1 文字  2 图片  3 产品 
							msgcontent: JSON.stringify(dataObj),
						})
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			/**
			 * types   消息类型
			 * params  content内容
			 * msg     此参数用作本地预发消息的  loading形式消息用
			 * */
			async sendmsg(data) {
				this.sendLoading = true;
				const { success, msg } = await sendmsg(data)
				this.sendLoading = false;
				if (success) {
					this.content = ''
				} else {
					this.Toast(msg)
				}
			},
		},
	}
</script>

<style lang="scss">
	.actionbar {
		.send-box {
			padding: 27rpx 24rpx;
			align-items: flex-end;

			.left {
				margin-right: 26rpx;
				padding-bottom: 8rpx;
			}

			.center {
				position: relative;
				flex: 1;

				textarea {
					position: relative;
					z-index: 1;
					width: 100%;
					background: rgba(232, 232, 232, .5);
					border-radius: 10rpx;
					padding: 10rpx 24rpx;
					box-sizing: border-box;
					color: #333;
				}

				.disabled {
					position: absolute;
					width: 100%;
					height: 100%;
					left: 0;
					top: 0;
					z-index: 9;

					text {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #999999;
					}
				}
			}

			.right-01 {
				margin-left: 26rpx;
				margin-bottom: 8rpx;
			}

			.right {
				align-items: center;
				margin-left: 26rpx;

				.m-button {
					opacity: 0;
					display: inline-flex;
					height: 62rpx;
					width: 0rpx;
					min-width: 0;
					padding: 0;
					overflow: hidden;
					white-space: nowrap;
					transition: all .3s;
				}

				.zone {
					width: 40rpx;
					opacity: 1;
					transition: all .5s;
				}

				// grid-template-columns: 1fr;
				// grid-gap: 26rpx;
			}

			.active {
				.m-button {
					width: 120rpx;
					transition: all .3s;
					opacity: 1;
				}

				.zone {
					width: 0rpx;
					opacity: 0;
				}
			}
		}

		.operation {
			height: 0;
			background: var(--bg);
			overflow: hidden;
			padding: 0 24rpx;
			transition: all .2s;

			.card+.card {
				padding-top: 35rpx;
			}
		}

		.operation-active {
			height: 260rpx;
			padding: 24rpx;
		}

	}

	.Incoming {
		background-color: #e9e9e9;
		padding: 8rpx 12rpx;
		border-radius: 8rpx;
		color: #777;
	}
</style>